﻿/*  Copyright 2012 PerceiveIT Limited
 *  This file is part of the Scryber library.
 *
 *  You can redistribute Scryber and/or modify 
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  Scryber is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Scryber source code in the COPYING.txt file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Scryber
{
    /// <summary>
    /// A stack of data and source instances that
    /// </summary>
    public class PDFDataStack
    {
        private Stack<object> stack = new Stack<object>();
        private Stack<IPDFDataSource> sources = new Stack<IPDFDataSource>();
        
        /// <summary>
        /// Returns true if there is at least one object on the data stack
        /// </summary>
        public bool HasData
        {
            get { return this.stack != null && this.stack.Count > 0; }
        }

        /// <summary>
        /// Gets the current object on this data stack
        /// </summary>
        public object Current
        {
            get
            {
                if (this.HasData == false)
                    throw new InvalidOperationException(Errors.NoDataContextOnTheStack);
                return this.stack.Peek();
            }
        }

        public IPDFDataSource Source
        {
            get
            {
                if (this.HasData == false)
                    throw new InvalidOperationException(Errors.NoDataContextOnTheStack);
                return this.sources.Peek();
            }
        }

        /// <summary>
        /// Pushes a new object onto the data stack
        /// </summary>
        /// <param name="data"></param>
        public void Push(object data, IPDFDataSource source)
        {
            if (null == source)
                throw new ArgumentNullException("source");

            this.stack.Push(data);
            this.sources.Push(source);
        }

        /// <summary>
        /// Pops (and returns) the current data object from the stack
        /// </summary>
        /// <returns></returns>
        public object Pop()
        {
            if(this.HasData == false)
                throw new InvalidOperationException(Errors.NoDataContextOnTheStack);
            this.sources.Pop();
            return this.stack.Pop();
        }


    }
}
